﻿using Equipment.API.Application.Service.DetectionmethodService;
using Equipment.API.DTOs.RequestDto.DefectlocationListRequest;
using Equipment.API.DTOs.ResponseDto.DefectlocationResponse;
using Equipment.Domain.Detectionmethod;
using Equipment.Errorcode;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Serilog;

namespace Equipment.API.Controllers
{
    /// <summary>
    /// 缺陷类型控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DefecttypeController : ControllerBase
    {
        private readonly DefecttypeService _defecttypeService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="defecttypeService"></param>
        public DefecttypeController(DefecttypeService defecttypeService)
        {
            _defecttypeService = defecttypeService;
        }
        /// <summary>
        /// 缺陷类型下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetDefecttypeDropdown()
        {
            return await _defecttypeService.GetDefecttypeDropdown();
        }

        /// <summary>
        /// 添加缺陷类型信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> AddDefecttype(AddDefecttypeResponseDto param)
        {
            Log.Information("缺陷信息新增成功！");
            return await _defecttypeService.AddDefecttype(param);
        }

        /// <summary>
        /// 获取缺陷类型信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetDefecttype([FromQuery]GetDefecttypeRequestDto dto, int pageNumber = 1, int pageSize = 4)
        {
            try
            {
                return await _defecttypeService.GetDefecttype(dto, pageNumber, pageSize);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "获取缺陷类型信息时发生错误。");
                return ApiResult.Fail("获取缺陷类型信息时发生错误。");
                throw;
            }

        }
        /// <summary>
        /// 修改缺陷类型信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ApiResult> UpdateDefecttype(UpdateDefecttypeRequestDto dto)
        {
            try
            {
                return await _defecttypeService.UpdateDefecttype(dto);
            }
            catch (Exception ex)
            {

                Log.Error(ex, "更新缺陷类型信息时发生错误。");
                return ApiResult.Fail("更新缺陷类型信息时发生错误。");
            }
        }
        /// <summary>
        /// 删除缺陷类型信息
        /// </summary>
        /// <param name="Defecttypeid"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ApiResult> DeleteDefecttype(int Defecttypeid)
        {
            try
            {
                return await _defecttypeService.DeleteDefecttype(Defecttypeid);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "删除缺陷类型信息时发生错误。");
                return ApiResult.Fail("删除缺陷类型信息时发生错误。");
            }
        }

    }
}
